package com.example.foo.member.service;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.member.model.MemberIdcard;
import com.example.foo.member.query.MemberIdcardQuery;
import com.example.foo.member.dao.MemberIdcardDao;

@org.springframework.stereotype.Service
public class MemberIdcardService {

	private final MemberIdcardDao memberIdcardDao;

	public MemberIdcardService(MemberIdcardDao memberIdcardDao) {
		this.memberIdcardDao = memberIdcardDao;
	}

	// ========== MemberIdcard ==========

	public PageResult<MemberIdcard> findMemberIdcards(MemberIdcardQuery memberIdcardQuery, Pageable pageable) {
		return memberIdcardDao.findMemberIdcards(memberIdcardQuery, pageable);
	}

	public List<MemberIdcard> findMemberIdcardsByIds(List<Long> ids) {
		return memberIdcardDao.selectBatchIds(ids);
	}

	public MemberIdcard findMemberIdcardById(Long id) {
		return memberIdcardDao.selectById(id);
	}

	public MemberIdcard findMemberIdcardByUserId(Long userId) {
		return memberIdcardDao.selectOne(new QueryWrapper<MemberIdcard>()
				.eq("user_id", userId)
				);
	}

	public void createMemberIdcardPending(Long userId) {
		MemberIdcard memberIdcard = new MemberIdcard();
		memberIdcard.setState(MemberIdcard.State.pending);
		memberIdcard.setIdcard("");
		memberIdcard.setIdname("");
		memberIdcard.setPicUrl1("");
		memberIdcard.setPicUrl2("");
		memberIdcard.setStartDate("");
		memberIdcard.setEndDate("");
		memberIdcard.setCellphone("");
		memberIdcard.setAddress("");
		memberIdcard.setMemo("");
		memberIdcard.setOuterMemberId("");
		memberIdcard.setUserId(userId);
		memberIdcard.setCreatedAt(LocalDateTime.now());
		memberIdcardDao.insert(memberIdcard);
	}

	public void updateMemberIdcardCellphone(Long id, String cellphone) {
		MemberIdcard memberIdcard = new MemberIdcard();
		memberIdcard.setId(id);
		memberIdcard.setCellphone(cellphone);
		memberIdcard.setUpdatedAt(LocalDateTime.now());
		memberIdcardDao.updateById(memberIdcard);
	}

	public void updateMemberIdcardBase(Long id, String idcard, String idname, String picUrl1, String picUrl2, String startDate, String endDate, String cellphone, String address) {
		MemberIdcard memberIdcard = memberIdcardDao.selectById(id);
		memberIdcard.setIdcard(idcard);
		memberIdcard.setIdname(idname);
		memberIdcard.setPicUrl1(picUrl1);
		memberIdcard.setPicUrl2(picUrl2);
		memberIdcard.setStartDate(startDate);
		memberIdcard.setEndDate(endDate);
		memberIdcard.setCellphone(cellphone);
		memberIdcard.setAddress(address);
		memberIdcard.setUpdatedAt(LocalDateTime.now());
		memberIdcardDao.updateById(memberIdcard);
	}

	public void updateMemberIdcardSuccess(Long id, String outerMemberId) {
		MemberIdcard memberIdcard = memberIdcardDao.selectById(id);
		memberIdcard.setState(MemberIdcard.State.success);
		memberIdcard.setOuterMemberId(outerMemberId);
		memberIdcard.setUpdatedAt(LocalDateTime.now());
		memberIdcardDao.updateById(memberIdcard);
	}

	public void deleteMemberIdcardById(Long id) {
		memberIdcardDao.deleteById(id);
	}

}
